// 主页数据模块
import { getGoodsDate, addGoods, searchGoodsIntheCar, reduceGoods, getGoodsDetails, addOrder, removeCar, getOrder, getOnesOrder, delOrder } from '@/api/api'

export default {
    namespaced: true,

    actions: {
        // 获取商品数据
        async getGoodsDate(content) {
            try {
                const res = await getGoodsDate()
                content.commit('GETGOODSDATE', res.data.docs)
            } catch (e) {
                console.log(e)
            }
        },

        // 添加商品到购物车
        async addGoods(content, item) {
            try {
                const res = await addGoods(item)
                if (res.msg === '无效Token') {
                    content.commit('TIPS', ' 请登陆后添加哦')
                } else {
                    content.commit('TIPS', res.msg)
                }
            } catch (e) {
                console.log(e)
            }
        },

        // 获取购物车商品
        async searchGoodsIntheCar(content) {
            try {
                const res = await searchGoodsIntheCar()
                content.commit('GETGOODSDATEINCAR', res.data.arr)
            } catch (e) {
                console.log(e)
            }
        },

        // 删除购物车商品
        async reduceGoods(content, item) {
            try {
                const res = await reduceGoods(item)
            } catch (e) {
                console.log(e)
            }
        },

        // 获取详情页数据
        async getGoodsDetails(content, _id) {
            try {
                const res = await getGoodsDetails(_id)
                content.commit('Details', res.data.goods)
            } catch (e) {
                console.log(e)
            }
        },

        // 添加订单
        async addOrder(content, info) {
            try {
                const res = await addOrder(info.sum, info.goods, info.address)
                content.commit('TIPS', res.msg)
            } catch (e) {
                console.log(e)
            }
        },


        // 清空购物车
        async removeCar(content) {
            try {
                const res = await removeCar()
            } catch (e) {
                console.log(e)
            }
        },


        // 获取订单信息
        async getOrder(content) {
            try {
                const res = await getOrder()
                content.commit('GETORDER', res.data.arr)
            } catch (e) {
                console.log(e)
            }
        },

        // 获取单个订单
        async getOnesOrder(content, _id) {
            try {
                const res = await getOnesOrder(_id)
                content.commit('GETONESORDER', res.data)
            } catch (e) {
                console.log(e)
            }
        },

        // 删除订单
        async delOrder(connect,_id){
            try{
                const res = await delOrder(_id)
                console.log(res)
                connect.commit('TIPS',res.msg)
            }catch(e){
                console.log(e)
            }
        }

    },

    mutations: {
        // 商品数据
        GETGOODSDATE(state, val) {
            state.goodsList = val.reverse()
        },

        // 提示框
        TIPS(state, val) {
            state.tips.tipText = val
            state.tips.isTips = true
            setTimeout(() => {
                state.tips.tipText = ''
                state.tips.isTips = false
            }, 1000)
        },

        // 购物车数据
        GETGOODSDATEINCAR(state, val) {
            state.shoppingCarList = val
        },

        // 详情页数据
        Details(state, val) {
            state.DetailsDate = val
        },

        // 订单数据
        GETORDER(state, val) {
            state.orderList = val
        },

        // 单个订单
        GETONESORDER(state, val) {
            state.onesOrder = val
        }
    },


    state: {
        // 商品数据
        goodsList: [],

        // 提示框
        tips: {
            tipText: '',
            isTips: false
        },

        // 购物车数据
        shoppingCarList: [],

        // 详情页数据
        DetailsDate: {},

        // 订单数据
        orderList: [],

        // 单个订单
        onesOrder: {},
    }
}